/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package hotel.jpa;

import entities.Room;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Query;
import javax.transaction.UserTransaction;

/**
 * This class handle all connection to the database
 * and returns information relataive to rooms. 
 * @author Anton Karlsson
 */
public class RoomJpaCtrl implements IRoomJpaCtrl<Room> {

    // UserTransaction not used in non container (i.e. for now)
    private final UserTransaction utx;
    private final EntityManagerFactory emf;

    public RoomJpaCtrl(UserTransaction utx, EntityManagerFactory emf) {
        this.utx = utx;
        this.emf = emf;
    }

    @Override
    public void create(Room t) {
        EntityManager em = emf.createEntityManager();
        EntityTransaction tx = em.getTransaction();
        tx.begin();
        em.persist(t);
        tx.commit();

        em.close();
    }

    @Override
    public void destroy(Long id) {
        EntityManager em = emf.createEntityManager();
        //We need to find right Booking 

        String query = "select r from Room r";
        Query q = em.createQuery(query);
        List<Room> result = q.getResultList();
        Room t = null;
        for (Room r : result) {
            if (r.getId().compareTo(id) == 0) {
                t = r;
            }
        }
        //Have right booking, now we only have to remove it

        EntityTransaction tx = em.getTransaction();
        tx.begin();
        em.remove(t);
        tx.commit();


        em.close();
    }

    @Override
    public void edit(Room t) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public Room findEntity(Room t) throws NonexistentEntityException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public List<Room> findEntities() {
        EntityManager em = emf.createEntityManager();

        //Find all users 
        String query = "select u from Room u";
        Query q = em.createQuery(query);
        List<Room> result = q.getResultList();

        em.close();

        return result;
    }

    @Override
    public EntityManager getEntityManager() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public int getEntityCount() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public List<Room> findAvailableRooms(List<Integer> roomNbrs) {
        EntityManager em = emf.createEntityManager();

        //Find all users 
        String query = "select u from Room u";
        Query q = em.createQuery(query);

        List<Room> result = q.getResultList();

        List<Room> finalResult = new ArrayList<Room>();

        for (Room r : result) {
            for (Integer nb : roomNbrs) {
                if (r.getRoomNb() != nb) {
                    finalResult.add(r);
                }
            }
        }

        if (finalResult.isEmpty()) {
            finalResult = result;
        }

        em.close();

        return finalResult;
    }
}
